#
# conf parsing functions (not public)
#
# $Id: conf.h 120 2011-02-17 09:11:00Z mamartynov@gmail.com $
#

##########################################################
# INITIALIZATION AND PRIVATE FUNCTIONS
##########################################################

_INCLUDE_CONF="OK"

function get_xmlparam() {
    sed -n -e 's/.* '$1'="\([^"]*\).*/\1/p'
}

function format_user() {
while read line
do
    section_line="";
    begin_section=`echo $line | sed -n -e '/^\ *<user/p'`
    if [ ! -z "$begin_section" ]; then
	end_section=""
	while [ -z "$end_section" ]
	do
	    section_line="$section_line $line"
    	    end_section=`echo $line | sed -n -e '/\/>\ *$/p'`
	    [ ! -z "$end_section" ] && break;
	    read line
	done
	echo $section_line
    else
	echo $line
    fi	
done < $OPENARS_HOME/conf/users.xml
}

function format_host() {
while read line
do
    section_line="";
    begin_section=`echo $line | sed -n -e '/^\ *<host/p'`
    if [ ! -z "$begin_section" ]; then
	end_section=""
	while [ -z "$end_section" ]
	do
	    section_line="$section_line $line"
    	    end_section=`echo $line | sed -n -e '/\/>\ *$/p'`
	    [ ! -z "$end_section" ] && break;
	    read line
	done
	echo $section_line
    else
	echo $line
    fi	
done < $OPENARS_HOME/conf/hosts.xml
}

# initialization environment
function main_setenv() {
    [ ! -z "$_MAIN_SETENV" ] && return 0
    module_list=( "core" "oracle" "cmdb" "zabbix" )
    while read line
    do
	module=`echo $line | sed -n 's/<\([^ >]*\)[^/]*>$/\1/p'`
	[ -z "$module" ] && continue
	flag=`echo $module | tr [a-z] [A-Z]`_ENABLE
	if [ "`echo $line | get_xmlparam enable`" = N  ]; then
	    eval export $flag=N
    	    while read line
	    do
		[ "$line" = "</$module>" ] && break
	    done
	else
	    eval export $flag=Y
	    while read line
	    do
		[ "$line" = "</$module>" ] && break
		name=`echo $line | get_xmlparam "sname"` 
		value=`echo $line | get_xmlparam "value"`
		[ ! -z "$name" ] && eval export $name=$value
	    done
	fi
    done < $OPENARS_HOME/conf/main.xml
    export _MAIN_SETENV="OK"
}
